package com.miracle9.game.dao;

import com.miracle9.game.entity.CardDesk;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class CardDeskServiceDao extends SqlOperating<CardDesk, Integer> {

    public List<Integer> getDeskIdsByRoomId(int roomId){
        return (List<Integer>)getSession().createQuery("select id from CardDesk where roomId = ?").setParameter(0, roomId).list();
    }

    @Cacheable(value = {"springCache"}, key = "'getAllCardDesk'")
    public List<CardDesk> getCardDesks() {
        return q_CreateSqlList("from CardDesk  order by roomId desc,orderBy", new Object[0]);
    }

    @CacheEvict(value = {"springCache"}, key = "'getAllCardDesk'")
    public void ifUpdate(int id) {
        if (id == -1) {
            q_CreateSql("update CardDesk set sumYaFen=0,sumDeFen=0", new Object[0]).executeUpdate();
        } else {
            q_CreateSql("update CardDesk set sumYaFen=0,sumDeFen=0 where id=?", new Object[]{Integer.valueOf(id)}).executeUpdate();
        }
    }

    @CacheEvict(value = {"springCache"}, key = "'getAllCardDesk'")
    public void save(CardDesk cardDesk) {
        saveReturn(cardDesk);
    }

    @CacheEvict(value = {"springCache"}, key = "'getAllCardDesk'")
    public void updateUpdateById(int id, int index) {
        q_CreateSql("update CardDesk set update=? where id=?", new Object[]{Integer.valueOf(index), Integer.valueOf(id)}).executeUpdate();
    }

    public void updateStateById(int id, int state) {
        q_CreateSql("update CardDesk set state=? where id=?", new Object[]{Integer.valueOf(state), Integer.valueOf(id)}).executeUpdate();
    }

}

